package com.example.springinit2.service.impl;

import com.example.springinit2.service.MsmService;
import com.example.springinit2.utils.Constant;
import com.example.springinit2.utils.MySmsSingleSender;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
public class MsmServiceImpl implements MsmService {

  @Override
  public void sendMsm(String name,String phoneNumber) {
	try {
	  String[] params = {name,"李星星测试账号"};//数组具体的元素个数和模板中变量个数必须一致，例如事例中templateId:5678对应一个变量，参数数组中元素个数也必须是一个
	  MySmsSingleSender ssender = new MySmsSingleSender(Constant.appid, Constant.appkey);
	  SmsSingleSenderResult result = ssender.sendWithParam("86", Constant.phoneNumbers[0],
			  Constant.templateId, params, Constant.smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
	  System.out.println(result);
	} catch (HTTPException e) {
	  // HTTP响应码错误
	  e.printStackTrace();
	} catch (JSONException e) {
	  // json解析错误
	  e.printStackTrace();
	} catch (IOException e) {
	  // 网络IO错误
	  e.printStackTrace();
	}

  }
}
